const MyPromise = require("./mypromise2.0.js")

const p = new MyPromise(
    /* 任务执行器：3秒后随机履约或毁约 */
    (resolve, reject) => {
        setTimeout(() => {
            Math.random() > 0.1 ? resolve("data0") : reject(new Error("一张好人卡"));
        }, 3000);
    }
)
    .then(
        data => {
            console.log("then1:data=", data)

            /* 立即履约或毁约/数据形式 */
            // return "data from then1"
            // throw new Error("err from then1")

            /* 立即履约或毁约/Promise形式 */
            // return MyPromise.resolve("data from then1")
            // return MyPromise.reject("err from then1")

            /* 稍后履约或毁约/Promise形式 */
            return new MyPromise(
                (resolve, reject) => {
                    setTimeout(() => {
                        Math.random() > 0.7 ? resolve("data from then1") : reject(new Error("err from then1"));
                    }, 3000);
                }
            )

        }
    )
    .catch(
        err => {
            // 这里本质继续履约了一个undefined
            console.log("catch1:err=", err)
        }
    )
    .then(
        data => {
            console.log("then2:data=", data)
        }
    )
    .catch(
        err => {
            console.log("catch2:err=", err)
        }
    )
    .finally(
        /* 这里本质继续履约了一个undefined */
        () => {
            console.log("finally:game over!")

            return new MyPromise(resolve => setTimeout(() => {
                resolve("一颗彩蛋！")
            }, 5000))
        }
    )
    .then(
        data => console.log("没想到还有...", data, "See You Next Time!")
    )

// 打印一下MyPromise对象的回调队列
console.log("p.callbacks", p.callbacks);